package com.chuanzhi.demo1;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
* 1.分析以下需求，并用代码实现：
	(1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
	(2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
	(3)利用四种方式遍历Map集合中的内容，格式：key::value
* */
public class StudentTask {
    public static void main(String[] args) {
        HashMap<Student,String > map = new HashMap<>();

        Student student1 = new Student("小明",18);
        Student student2 = new Student("小王",17);
        Student student3 = new Student("小红",16);

        map.put(student1,"衡阳");
        map.put(student2,"长沙");
        map.put(student3,"湘潭");
        //通过获取键的set集合，在通过迭代器进行遍历
        Set<Student> mapStudent = map.keySet();
        Iterator<Student> it = mapStudent.iterator();
        while(it.hasNext()){
            Student next = it.next();
            String s = map.get(next);
            System.out.println(next.getName()+","+next.getAge()+"-->"+s);
        }
        System.out.println("-----------------------");
        //通过增强for进行遍历
        for (Student mp : mapStudent) {
            String vules = map.get(mp);
            System.out.println(mp+"-->"+vules);
        }
        System.out.println("-----------------------");
        //通过获取entry对象，在同迭代器进行遍历
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        Iterator<Map.Entry<Student, String>> iterator = entries.iterator();
        while(iterator.hasNext()){
            Map.Entry<Student, String> next1 = iterator.next();
            Student key = next1.getKey();
            String value = next1.getValue();
            System.out.println(key+"-->"+value);
        }
        System.out.println("-----------------------");
        for (Map.Entry<Student, String> mapEntry:entries ) {
            System.out.println(mapEntry.getKey()+"-->"+mapEntry.getValue());
        }
    }
}
